package flc.ast.fragment;

import android.graphics.Color;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FragmentUtils;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.FragmentFileBinding;
import flc.ast.dialog.SetAnswerDialog;
import flc.ast.dialog.SetPasswordDialog;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.SPUtil;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FileFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FileFragment extends BaseNoModelFragment<FragmentFileBinding> {

    private List<Fragment> mFragmentList;
    private PictureFragment pictureFragment;
    private AudioFragment audioFragment;

    @Override
    protected int onCreate() {
        return R.layout.fragment_file;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);

        mFragmentList = new ArrayList<>();
        pictureFragment = new PictureFragment();
        mFragmentList.add(pictureFragment);
        audioFragment = new AudioFragment();
        mFragmentList.add(audioFragment);
        FragmentUtils.add(getChildFragmentManager(), mFragmentList, R.id.fl_container, 0);

        mDataBinding.tvPictureLibrary.setOnClickListener(this);
        mDataBinding.tvAudio.setOnClickListener(this);

        mDataBinding.ivAddAlbum.setOnClickListener(this);
        mDataBinding.ivPrivacyFile.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvPictureLibrary://图库
                clearSelection();
                mDataBinding.tvPictureLibrary.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.tvPictureLibrary.setTextSize(24);
                mDataBinding.ivPictureLibrary.setVisibility(View.VISIBLE);
                FragmentUtils.showHide(0, mFragmentList);
                mDataBinding.ivAddAlbum.setVisibility(View.VISIBLE);
                break;
            case R.id.tvAudio://音频
                clearSelection();
                mDataBinding.tvAudio.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.tvAudio.setTextSize(24);
                mDataBinding.ivAudio.setVisibility(View.VISIBLE);
                FragmentUtils.showHide(1, mFragmentList);
                mDataBinding.ivAddAlbum.setVisibility(View.GONE);
                break;
            case R.id.ivAddAlbum://添加视频相册
                pictureFragment.showAddAlbumDialog();
                break;
            case R.id.ivPrivacyFile://加密文件
                //是否完成设置密码
                boolean isSetPassword = SPUtil.getBoolean(getContext(), Content.isHavePassword, false);
                if (isSetPassword) {
                    passwordDialog(false, "", "");
                } else {
                    answerDialog(false);
                }
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void answerDialog(boolean isForget) {
        SetAnswerDialog answerDialog = new SetAnswerDialog(mContext);
        answerDialog.isForget = isForget;
        answerDialog.setListener(new SetAnswerDialog.Listener() {
            @Override
            public void setPassword(String question, String answer) {
                passwordDialog(true, question, answer);
            }
        });
        answerDialog.show();
    }

    private void passwordDialog(boolean isSetPassword, String question, String answer) {
        SetPasswordDialog passwordDialog = new SetPasswordDialog(mContext);
        passwordDialog.isFile = true;
        passwordDialog.isSetPassword = isSetPassword;
        if (isSetPassword) {
            passwordDialog.question = question;
            passwordDialog.answer = answer;
        }
        passwordDialog.setListener(new SetPasswordDialog.Listener() {
            @Override
            public void forgetPassword() {
                answerDialog(true);
            }
        });
        passwordDialog.show();
    }

    private void clearSelection() {
        mDataBinding.tvPictureLibrary.setTextColor(Color.parseColor("#80FFFFFF"));
        mDataBinding.tvPictureLibrary.setTextSize(15);
        mDataBinding.ivPictureLibrary.setVisibility(View.GONE);
        mDataBinding.tvAudio.setTextColor(Color.parseColor("#80FFFFFF"));
        mDataBinding.tvAudio.setTextSize(15);
        mDataBinding.ivAudio.setVisibility(View.GONE);
    }

}